﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace BanXeTang
{
    public class MyTank : Tank
    {
        public int mang;
        public int maxMang;
        public override VisibleGameEntity Clone()
        {
            VisibleGameEntity newObject = new MyTank();
            newObject._nsprite = this._nsprite;
            newObject._sprite = this._sprite;
            newObject.X = this.X;
            newObject.Y = this.Y;
            ((MyTank)newObject).nDelay = this.nDelay;
            ((MyTank)newObject).iDelay = 0;
            ((MyTank)newObject).huong = 1;
            ((MyTank) newObject).nDoTre = nDoTre;
            ((MyTank)newObject).tocDo = tocDo;
            ((MyTank) newObject).maxMau = maxMau;
            ((MyTank) newObject).mau = maxMau;
            ((MyTank) newObject).maxMang = maxMang;
            ((MyTank) newObject).mang = maxMang;
            
            return newObject;
        }
        public override void Update(GameTime gameTime)
        {
            Input.keyboardState = Keyboard.GetState();
            if (Input.keyboardState.IsKeyDown(Keys.A) && !_dan.duocBan && tiepTucBan)
                Ban();
            _dan.Update(gameTime);
            if (iDoTre == 0)
                tiepTucBan = true;
            iDoTre = (iDoTre + 1) % nDoTre;
         
            //Khi nhan nut xuong);
            if (Input.keyboardState.IsKeyDown(Keys.Down))
            {
                huong = 2;
                Y = Y + tocDo;
                if (Y > GlobalSettings._curMapHeight - _sprite[huong].texture2d[0].Height)
                    Y = GlobalSettings._curMapHeight - _sprite[huong].texture2d[0].Height;
                Game1.tankTa.Y = Y;
                if (iDelay == 0)
                    _sprite[huong].Update(gameTime);
                iDelay = (iDelay + 1) % nDelay;
                return;
            }

            //Khi nhấn nút lên
            if (Input.keyboardState.IsKeyDown(Keys.Up))
            {
                huong = 0;
                Y = Y - tocDo;
                if (Y < 0)
                    Y = 0;
                Game1.tankTa.Y = Y;
                if (iDelay == 0)
                    _sprite[huong].Update(gameTime);
                iDelay = (iDelay + 1) % nDelay;
                return;
            }


            //Khi nhấn nút trái
            if (Input.keyboardState.IsKeyDown(Keys.Left))
            {
                huong = 3;
                X = X - tocDo;
                if (X < 0)
                    X = 0;
                Game1.tankTa.X = X;
                if (iDelay == 0)
                    _sprite[huong].Update(gameTime);
                iDelay = (iDelay + 1) % nDelay;
                return;
            }


            //Khi nhấn nút phải
            if (Input.keyboardState.IsKeyDown(Keys.Right))
            {
                huong = 1;
                X = X + tocDo;
                if (X > GlobalSettings._curMapWidth - _sprite[huong].texture2d[0].Width)
                    X = GlobalSettings._curMapWidth - _sprite[huong].texture2d[0].Width;
                Game1.tankTa.X = X;
                if (iDelay == 0)
                    _sprite[huong].Update(gameTime);
                iDelay = (iDelay + 1) % nDelay;
                return;
            }
          
        }

        public override void Reset()
        {
            X = 0;
            Y = 0;
            mau = maxMau;
            GlobalSettings._DeltaX = 0;
            GlobalSettings._DeltaY = 0;
        }

        public bool CheckCollisions(VatPham vp)
        {
            for (int i = 0; i < _nsprite; i++)
            {               
                    if (_sprite[i].CheckCollisions(vp.SpriteVatPham))
                    {
                        return true;
                    }   
            }
            return false;
        }

        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            _sprite[huong].Draw(gameTime, spriteBatch, Color.White);
            _dan.Draw(gameTime, spriteBatch);
        }

        public void VeOngMau(SpriteBatch spriteBatch)
        {
            if (mau >= 1)
                spriteBatch.Draw(GlobalSettings.binhMauTa[mau - 1], new Vector2(Game1.screenWidth - GlobalSettings.binhMauTa[mau - 1].Width - 2, 0), Color.White);
        }

        public void VeMang(SpriteBatch spriteBatch)
        {
            for(int i = 1; i <= mang; i++)
            {
                spriteBatch.Draw(GlobalSettings.mang, new Vector2((Game1.screenWidth - GlobalSettings.binhMauTa[mau - 1].Width - 5 - (GlobalSettings.mang.Width * i)), 0), Color.White);
            }
        }

    }
}
